/**

 @author 石头哥哥
 </P>
 Date:   2014/12/17
 </P>
 Time:   21:13

 注解：

 */
package {
import GameObject.Enemy;


import starling.animation.IAnimatable;

import starling.core.Starling;

import starling.display.Sprite;

import utils.Kit;


/**
 * 游戏中 创建敌机 工厂
 */
public class EnemyManager {

    private static var instance:EnemyManager;
    private static var inited:Boolean = false;

    public function EnemyManager() {
        if (inited)throw new Error("EnemyManager is singleton ,don't create anymore");
    }

    /**
     * 返回 工厂对象实体
     * @return
     */
    public static function getInstance():EnemyManager {
        if (!inited) {
            instance = new EnemyManager();
            inited = true;
        }
        return instance;
    }


    /**
     * 游戏场景准备完毕，开始游戏
     * 创建对应的enemy
     */
    private var createAnimatable:IAnimatable;
    private var gameRoot:Sprite;

    /**
     *
     * @param parent
     */
    public function start(parent:Sprite):void {
        createAnimatable = Starling.juggler.repeatCall(createEnemyCall, 2);
        gameRoot = parent;
    }

    /**
     * 创建
     * @param type     1,2,3
     * @param hp       1~10
     * @param bulletType   1
     * @param x_postion
     * @param y_postion
     * @param parent
     * @param bulletSpeed   1~10
     */
    private function createEnemyCall():void {
        var enemy:Enemy = new Enemy(gameRoot, Kit.Rnd(1, 3), 0, 0, 1, Kit.Rnd(5, 10));
        if (enemy) {
            enemy.init();
            GameManger.enemyList.push(enemy);
            gameRoot.addChild(enemy);
            enemy.fire();
        } else {
            throw Error("create enemy error");
        }
    }

    /**
     * 停止Enemy create
     */
    public function stop():void {
        if (createAnimatable)Starling.juggler.remove(createAnimatable);
    }

    public function pause():void {
        if (createAnimatable)Starling.juggler.remove(createAnimatable);
    }


    public function reuse():void {
        if (createAnimatable)Starling.juggler.add(createAnimatable);
    }

}
}
